package com.CST.controller.zx;


import com.CST.common.api.ResultObject;
import com.CST.entity.zx.ZxExpenseType;
import com.CST.service.zx.IZxExpenseTypeService;
import com.CST.util.ExcelTransfer;
import io.swagger.annotations.*;

import org.springframework.beans.factory.annotation.*;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;

/**
 * @author ly
 * @since 2023-03-03
 */
@Api(tags = {"科目关系对照"})
@RestController
@RequestMapping("/zxExpenseType")

public class ZxExpenseTypeController {

    @Autowired
    private IZxExpenseTypeService targetService;

    @PostMapping("/page")
    @ApiOperation("跳转到首页，必传参数pageNo 第几页，pageSize 每页几条" +
            "选择传递参数 accountCode 科目编码，accountName 科目名称，projectTypeCode 项目类别编号，expenseTypeCode 费用类别编号，reExTypeCode 收支类型编号")
    public ResultObject page(@RequestBody ZxExpenseType zxExpenseType) {
        return targetService.myPage(zxExpenseType);
    }

    @PostMapping("/saveOrUpdate")
    @ApiOperation("编辑的保存按钮绑定事件，带id就是更新，没有就是保存")
    public ResultObject saveOrUpdate(@RequestBody ZxExpenseType zxExpenseType) {
        return targetService.saveOrUpdateZxExpense(zxExpenseType)?ResultObject.success("修改成功"):ResultObject.failed("修改失败");
    }

    @DeleteMapping("/delete/{id}")
    @ApiOperation("逻辑删除费用类别管理，必要参数id")
    public ResultObject delete(@PathVariable Long id) {
        return targetService.removeById(id)?ResultObject.success("删除成功"):ResultObject.failed("删除失败");
    }

    @PostMapping("/download/{isMuBan}")
    @ApiOperation("下载数据，必传参数ismuban 0 模版 1 全部数据 ZxExpenseType这个下载模版是可以是空的" +
            "选择传递参数 accountCode 科目编码，accountName 科目名称，projectTypeCode 项目类别编号，expenseTypeCode 费用类别编号，reExTypeCode 收支类型编号")
//    条件存在问题@RequestBody ZxExpenseType zxExpenseType
    public void downLoad(HttpServletResponse response, @PathVariable Integer isMuBan,@RequestBody ZxExpenseType zxExpenseType){
        if (isMuBan == 0){
            ExcelTransfer<ZxExpenseType> excelTransfer = new ExcelTransfer<>();
            try {
                excelTransfer.exportExcel(response,null,"科目关系对照","科目关系对照明细", ZxExpenseType.class);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
            return;
        }
        targetService.download(response,zxExpenseType);
    }

    @PostMapping("/saveFile")
    @ApiOperation("导入文件，判重只要编码不一致就认为不一样")
    public ResultObject saveFile(MultipartFile file) {
        targetService.saveFile(file);
        return ResultObject.successReturn("导入成功");
    }
}

